package com.open4cn.simulator.answer.xrite;

import com.open4cn.simulator.core.answer.AbstractAnswer;
import com.open4cn.simulator.core.answer.AnswerModeEnum;
import com.open4cn.simulator.core.answer.AnswerResponse;
import com.open4cn.simulator.core.ext.XriteStatus;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-13 01:41
 */
public class ChangeUVToCalExtAnswer extends AbstractAnswer {

    public ChangeUVToCalExtAnswer() {
        super("xrite/changeUVToCalExt", AnswerModeEnum.SUCCESS);
    }


    public AnswerResponse answer(XriteStatus xriteStatus, String instruction) {
        if (!AnswerModeEnum.FAIL.equals(getAnswerMode())) {
            getActions().forEach(action -> {
                action.action(xriteStatus, instruction);
            });
        }
        AnswerResponse response = getAnswer(xriteStatus);
        return response;
    }

    /**
     * 获取应答结果
     *
     * @param xriteStatus
     * @return
     */
    private AnswerResponse getAnswer(XriteStatus xriteStatus) {
        switch (getAnswerMode()) {
            case SUCCESS:
                return AnswerResponse.success(success(xriteStatus));
            case FAIL:
                return AnswerResponse.fail(fail(xriteStatus));
            case LOOP:
                return AnswerResponse.success(getLoop());
            case INPUT:
                Object input = pollInput();
                if (null != input) {
                    return AnswerResponse.success(input);
                }
            default:
                return AnswerResponse.success(success(xriteStatus));
        }
    }


    /**
     * 成功应答
     * 110020005700000'\x0d‘'\x0a’>
     *
     * @param xriteStatus
     * @return
     */
    private Object success(XriteStatus xriteStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("1")
                .append("10")
                .append(xriteStatus.getCurrentSpecularGloss().getCode())
                .append(xriteStatus.getCurrentUV().getCode())
                .append(xriteStatus.getCurrentAperture().getCode())
                .append("005700000")
                .append("\r\n")
                .append(">");
        return builder.toString();
    }


    /**
     * 失败应答
     * 9102123000000000'\x0d‘'\x0a’>
     *
     * @param xriteStatus
     * @return
     */
    private Object fail(XriteStatus xriteStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("9")
                .append("10")
                .append(xriteStatus.getCurrentSpecularGloss().getCode())
                .append(xriteStatus.getCurrentUV().getCode())
                .append(xriteStatus.getCurrentAperture().getCode())
                .append("005700000")
                .append("\r\n")
                .append(">");
        return builder.toString();
    }
}
